package cn.gson.springboot.controllers;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.gson.springboot.model.pojos.Category;
import cn.gson.springboot.model.pojos.Drug;
import cn.gson.springboot.model.pojos.Indent;
import cn.gson.springboot.model.pojos.Inventory;
import cn.gson.springboot.model.pojos.Supplier;
import cn.gson.springboot.model.service.DrugstorageService;
import cn.gson.springboot.model.service.IndentService;
import cn.gson.springboot.model.service.SupplierService;
import cn.gson.springboot.model.vo.AuditVo;
import cn.gson.springboot.model.vo.DestructionVo;
import cn.gson.springboot.model.vo.DrugscheckVo;
import cn.gson.springboot.model.vo.IndentVo;
import cn.gson.springboot.model.vo.OutboundAo;
import cn.gson.springboot.model.vo.ReturndrugVo;

@Controller
public class DrugStorageController {
		
		@Autowired
		private  DrugstorageService drugstorageservice ; //药品
		
		@Autowired
		private  IndentService indentservice ; //采购订单
		
		@Autowired
		private  SupplierService supplierservice ; //供应商
	
		// 药品信息
		@RequestMapping("ph-drug")
		public void phDrug() {
	
		}
		
		//添加药品
		@PostMapping("add-drug")
		@ResponseBody
		public String  AddDrug(Drug drug) {
			try {
				boolean findByss = indentservice.findByss(drug.getDrugName(),drug.getDrugStandard(),drug.getSupplier().getSupplierNumber(),drug.getType().getTypeId());
				if(findByss==true) {
					return "1";
				}
				if(findByss==false) {
					drugstorageservice.AddDrug(drug);
					return "3";
				}
			 
			  return null;	
				
			} catch (Exception e) {
				e.printStackTrace();
				return "2";
			}
			
		}

		//查询所有药品
		@RequestMapping("findall-drug")
		@ResponseBody
		public  Map<String,Object>  Findalldrug(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows, String drugname){

				//进行分页
				Page<Object> pg = PageHelper.startPage(page, rows);
				JSONArray findAllDrug = drugstorageservice.FindAllDrug(drugname);

				//进行数据格式转换和封装
				Map<String,Object> map = new HashMap<>();
				map.put("total", pg.getTotal());//封装总记录数
				map.put("rows", findAllDrug);//封装当前页的内容
	
				return map;
				
		}
		
		//根据药品编号查询药品详细批次
		@PostMapping("find-drugbatch")
		@ResponseBody
		public  String  finddrugbatch(Integer drugnumber){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = drugstorageservice.finddrugbatch(drugnumber);
				json.put("result", jsoninput);
				return json.toJSONString();			
		}	
		
		//显示药品类别
		@RequestMapping("drug_category")
		@ResponseBody
		public JSONArray drugCategory(){
			return drugstorageservice.FindByCatefory();
		}	
		
		//显示药品类型
		@PostMapping("drug_type")
		@ResponseBody
		public JSONArray FindByType(Integer categoryid){
			return drugstorageservice.FindByType(categoryid);
		}

		// 采购管理
		@RequestMapping("ph-procurement")
		public void phProcurement() {
	
		}
		//显示供应商
		@RequestMapping("all-supplier")
		@ResponseBody
		public JSONArray allsupplier(){
			return drugstorageservice.allsupplier();
		}
		
		//显示供应商药品
		@PostMapping("find-supplier")
		@ResponseBody
		public  Map<String,Object>  findsuplier(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows,Integer supplierno){
			
				//进行分页
				Page<Object> pg = PageHelper.startPage(page, rows);
				JSONArray findAllIndent = drugstorageservice.findsuplier(supplierno);
				//进行数据格式转换和封装
				Map<String,Object> map = new HashMap<>();
				map.put("total", pg.getTotal());//封装总记录数
				map.put("rows", findAllIndent);//封装当前页的内容
				
				return map;			
		}		
		
		//显示采购订单
		@RequestMapping("findall-indent")
		@ResponseBody
		public  Map<String,Object>  Findallindent(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows,Integer indentid,String oday,String tday){
			
				//进行分页
				Page<Object> pg = PageHelper.startPage(page, rows);
				JSONArray findAllIndent = indentservice.FindAllIndent(indentid, oday, tday);
				//进行数据格式转换和封装
				Map<String,Object> map = new HashMap<>();
				map.put("total", pg.getTotal());//封装总记录数
				map.put("rows", findAllIndent);//封装当前页的内容
				
				return map;			
		}		
		
		//根据采购单号采购订单详情
		@PostMapping("findall-indentdetails")
		@ResponseBody
		public  String  Findallindentdetails(Integer indentid){
				JSONObject  json = new JSONObject();
				JSONArray jsonindent = indentservice.findallindentdetails(indentid);
				json.put("result", jsonindent);
				return json.toJSONString();			
		}		

		// 入库管理
		@RequestMapping("ph-Putinstorage")
		public void phPutinstorage() {
	
		}
		
		//显示入库单
		@RequestMapping("findall-PutInStorage")
		@ResponseBody
		public  Map<String,Object>  FindAllPutInStorage(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows,Integer inid,String aday,String bday){
			
				//进行分页
				Page<Object> pg = PageHelper.startPage(page, rows);
				JSONArray findAllPutInStorage = indentservice.FindAllPutInStorage(inid, aday, bday);
				//进行数据格式转换和封装
				Map<String,Object> map = new HashMap<>();
				map.put("total", pg.getTotal());//封装总记录数
				map.put("rows", findAllPutInStorage);//封装当前页的内容
				
				return map;			
		}		
		
		//根据入库单号查询订单详情
		@PostMapping("findBy-PutInStorage")
		@ResponseBody
		public  String  FindByPutInStorage(Integer instorageid){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = indentservice.FindByPutInStorage(instorageid);
				json.put("result", jsoninput);
				return json.toJSONString();			
		}		

		// 出库管理
		@RequestMapping("ph-outbound")
		public void phOutbound() {
	
		}	
		
		//显示出库单
		@PostMapping("find-outbound")
		@ResponseBody
		public  Map<String,Object>  FindAlloutbound(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows,Integer outboundnumber,String qday,String wday){
			
				//进行分页
				Page<Object> pg = PageHelper.startPage(page, rows);
				JSONArray findAlloutbound = indentservice.FindAlloutbound(outboundnumber, qday, wday);
				//进行数据格式转换和封装
				Map<String,Object> map = new HashMap<>();
				map.put("total", pg.getTotal());//封装总记录数
				map.put("rows", findAlloutbound);//封装当前页的内容
				
				return map;			
		}		
		
		//根据出库单号查询出库详情
		@PostMapping("findBy-outbound")
		@ResponseBody
		
		public  Map<String,Object>  FindByOutbound(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows,Integer outboundid){
			
				//进行分页
				Page<Object> pg = PageHelper.startPage(page, rows);
				JSONArray findAllIndent = indentservice.FindByOutbound(outboundid);
				//进行数据格式转换和封装
				Map<String,Object> map = new HashMap<>();
				map.put("total", pg.getTotal());//封装总记录数
				map.put("rows", findAllIndent);//封装当前页的内容
				
				return map;			
		}		

		// 入库审核
		@RequestMapping("ph-audit")
		public void phAudit() {
	
		}
		
		//入库审核显示待验收采购订单
		@RequestMapping("findother-indent")
		@ResponseBody
		public  Map<String,Object>  Findotherindent(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows){
			
				//进行分页
				Page<Object> pg = PageHelper.startPage(page, rows);
				JSONArray findOtherIndent = indentservice.FindOtherIndent();
				
				//进行数据格式转换和封装
				Map<String,Object> map = new HashMap<>();
				map.put("total", pg.getTotal());//封装总记录数
				map.put("rows", findOtherIndent);//封装当前页的内容
								
				return map;
				
		}		
		
		//开始药品入库
		@PostMapping("save-inventory")
		@ResponseBody
		public String saveByinventory(AuditVo auditVo) {
			try {
				indentservice.savePut(auditVo);
				return "1";
			} catch (Exception e) {
				e.printStackTrace();
				return "2";
			}
		}
		
		//修改订单状态
		@PostMapping("update-indent")
		@ResponseBody
		public  JSONArray  UpdateIndent(Integer indentid){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = indentservice.updateindent(indentid);
				return jsoninput;
		}		
		
		//入库修改库存数量
		@PostMapping("update-inven")
		@ResponseBody
		public  JSONArray  updateinven(Integer inventorynumber,Integer sum){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = indentservice.updateinven(inventorynumber,sum);
				return jsoninput;
		}		

		// 出库审核
		@RequestMapping("ph-review")
		public void phReview() {
	
		}
		
		//显示出库单
		@RequestMapping("findall-apply")
		@ResponseBody
		public  Map<String,Object>  Findapply(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows){
			
				//进行分页
				Page<Object> pg = PageHelper.startPage(page, rows);
				JSONArray findAllapply = indentservice.Findapply();
				//进行数据格式转换和封装
				Map<String,Object> map = new HashMap<>();
				map.put("total", pg.getTotal());//封装总记录数
				map.put("rows", findAllapply);//封装当前页的内容
				
				return map;			
		}		
		
		//修改请领状态
		@PostMapping("update-apply")
		@ResponseBody
		public  JSONArray  updateapply(Integer applyid){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = indentservice.updateapply(applyid);
				return jsoninput;
		}		
		
		//开始药品发货
		@PostMapping(value="save-Outbound")
		@ResponseBody
		public String saveOutbound(OutboundAo OutboundAo) {
			try {
				indentservice.saveOutbound(OutboundAo);
				return "1";
			} catch (Exception e) {
				e.printStackTrace();
				return "2";
			}
		}
		
		//出库修改库存数量
		@PostMapping("update-dinvent")
		@ResponseBody
		public  JSONArray  updatedinvent(Integer inventorynumber,Integer sum){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = indentservice.updatedinvent(inventorynumber,sum);
				return jsoninput;
		}		
		
		//出库修改请领详情实际发货数量
		@PostMapping("update-applaydis")
		@ResponseBody
		public  JSONArray  updateaplaydis(Integer adetailsid,Integer sum){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = indentservice.updateaplaydis(adetailsid,sum);
				return jsoninput;
		}	
		
		//根据入库单号查询订单详情
		@PostMapping("findBy-applydet")
		@ResponseBody
		public  String  Findapplydet(Integer drugapplyid){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = indentservice.Findapplydet(drugapplyid);
				json.put("result", jsoninput);
				return json.toJSONString();			
		}		

		// 药品采购
		@RequestMapping("ph-purchase")
		public void phPurchase() {
	
		}
		
		//开始药品采购
		@PostMapping(value="save-Bydrug")
		@ResponseBody
		public String saveBydrug(IndentVo indentVo) {
			try {
				indentservice.saveOrder(indentVo);
				return "1";
			} catch (Exception e) {
				e.printStackTrace();
				return "2";
			}
		}

		// 药品退药
		@RequestMapping("ph-returndrug")
		public void phReturndrug() {
	
		}
		
		//开始药品退药
		@PostMapping("save-Return")
		@ResponseBody
		public String saveReturn(ReturndrugVo ReturndrugVo) {
			try {
				indentservice.saveReturn(ReturndrugVo);
				return "1";
			} catch (Exception e) {
				e.printStackTrace();
				return "2";
			}
		}
		
		//退药修改库存数量
		@PostMapping("update-kucun")
		@ResponseBody
		public  JSONArray  updatekucun(Integer inventorynum,Integer sum){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = indentservice.updatekucun(inventorynum,sum);
				return jsoninput;
		}		
		
		//退药修改库存数量
		@PostMapping("update-inpici")
		@ResponseBody
		public  JSONArray  updateinpici(Integer inventorynum,Integer sum,Integer batch){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = indentservice.updateinpici(inventorynum,sum,batch);
				return jsoninput;
		}		
		
		//修改退药状态
		@PostMapping("update-return")
		@ResponseBody
		public  JSONArray  updatereturn(Integer returngoid,String checkpeople){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = indentservice.updatereturn(returngoid,checkpeople);
				return jsoninput;
		}		

		
		//显示所有药品批次
		@RequestMapping("select-drugs")
		@ResponseBody
		public  Map<String,Object>  selectselectdrugs(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows,Integer supplierno,Integer days,Integer dada,Integer dddd){
			
				//进行分页
				Page<Object> pg = PageHelper.startPage(page, rows);
				JSONArray findAldrugs= indentservice.selectdrugs(supplierno,days,dada,dddd);
				//进行数据格式转换和封装
				Map<String,Object> map = new HashMap<>();
				map.put("total", pg.getTotal());//封装总记录数
				map.put("rows", findAldrugs);//封装当前页的内容
				
				return map;			
		}		
		
		//查询退药记录
		@RequestMapping("find-returndrugjl")
		@ResponseBody
		public  Map<String,Object>  findreturndrugjl(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows){
			
				//进行分页
				Page<Object> pg = PageHelper.startPage(page, rows);
				JSONArray findOtherIndent = drugstorageservice.findreturndrugjl();
				
				//进行数据格式转换和封装
				Map<String,Object> map = new HashMap<>();
				map.put("total", pg.getTotal());//封装总记录数
				map.put("rows", findOtherIndent);//封装当前页的内容
								
				return map;
				
		}		
		
		//查询退药表
		@RequestMapping("find-returndrug")
		@ResponseBody
		public  Map<String,Object>  findreturndrug(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows){
			
				//进行分页
				Page<Object> pg = PageHelper.startPage(page, rows);
				JSONArray findOtherIndent = drugstorageservice.findreturndrug();
				
				//进行数据格式转换和封装
				Map<String,Object> map = new HashMap<>();
				map.put("total", pg.getTotal());//封装总记录数
				map.put("rows", findOtherIndent);//封装当前页的内容
								
				return map;
				
		}		
		
		//根据退药单号查询退药详情
		@PostMapping("findBy-turndrugjl")
		@ResponseBody
		public  String  findturndrugjl(Integer returngoodsid){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = drugstorageservice.findturndrugjl(returngoodsid);
				json.put("result", jsoninput);
				return json.toJSONString();			
		}		

		// 药品销毁
		@RequestMapping("ph-destruction")
		public void phDestruction() {
	
		}
		
		//开始药品销毁
		@PostMapping("save-Destruction")
		@ResponseBody
		public String saveDestruction(DestructionVo DestructionVo) {
			try {
				indentservice.saveDestruction(DestructionVo);
				return "1";
			} catch (Exception e) {
				e.printStackTrace();
				return "2";
			}
		}
		
		//查询销毁记录
		@RequestMapping("find-destroyjl")
		@ResponseBody
		public  Map<String,Object>  finddestroyjl(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows){
			
				//进行分页
				Page<Object> pg = PageHelper.startPage(page, rows);
				JSONArray findOtherIndent = drugstorageservice.finddestroyjl();
				
				//进行数据格式转换和封装
				Map<String,Object> map = new HashMap<>();
				map.put("total", pg.getTotal());//封装总记录数
				map.put("rows", findOtherIndent);//封装当前页的内容
								
				return map;
				
		}		
		
		//查询销毁表
		@RequestMapping("find-destroy")
		@ResponseBody
		public  Map<String,Object>  finddestroy(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows){
			
				//进行分页
				Page<Object> pg = PageHelper.startPage(page, rows);
				JSONArray findOtherIndent = drugstorageservice.finddestroy();
				
				//进行数据格式转换和封装
				Map<String,Object> map = new HashMap<>();
				map.put("total", pg.getTotal());//封装总记录数
				map.put("rows", findOtherIndent);//封装当前页的内容
								
				return map;
				
		}		
		
		//根据销毁单号查询销毁详情
		@PostMapping("find-destroydetails")
		@ResponseBody
		public  String  finddestroydetails(Integer destroyid){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = drugstorageservice.finddestroydetails(destroyid);
				json.put("result", jsoninput);
				return json.toJSONString();			
		}	
		
		//销毁修改库存数量
		@PostMapping("update-outkucun")
		@ResponseBody
		public  JSONArray  updateoutkucun(Integer inventorynum,Integer sum){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = indentservice.updateoutkucun(inventorynum,sum);
				return jsoninput;
		}		
		
		//销毁修改库存数量
		@PostMapping("update-outpici")
		@ResponseBody
		public  JSONArray  updateoutpici(Integer inventorynum,Integer sum,Integer batch){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = indentservice.updateoutpici(inventorynum,sum,batch);
				return jsoninput;
		}		
		
		//修改销毁状态
		@PostMapping("update-destroy")
		@ResponseBody
		public  JSONArray  updatedestroy(Integer destroyid,String checkpeople){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = indentservice.updatedestroy(destroyid,checkpeople);
				return jsoninput;
		}		

		// 药品盘点
		@RequestMapping("ph-takestock")
		public void phTakestock() {
	
		}
		
		//开始药品盘点
		@PostMapping("save-Check")
		@ResponseBody
		public String saveCheck(Integer caty,String name) {
			String type = null;
			try {
				if(caty==1) {
					type = "中药房" ;
					boolean panduan = indentservice.takes(type);
					if(panduan==true) {
						return "3";
					}
					if(panduan==false) {
						indentservice.Drugscheck(caty,name);
						return "1";
					}
				}
				if(caty==2) {
					type = "西药房" ;
					boolean panduan = indentservice.takes(type);
					if(panduan==true) {
						return "3";
					}
					if(panduan==false) {
						indentservice.Drugscheck(caty,name);
						return "1";
					}
				}
				
				return "0";
			} catch (Exception e) {
				e.printStackTrace();
				return "2";
			}
		}
		
		//查询盘点表
		@RequestMapping("find-checktable")
		@ResponseBody
		public  Map<String,Object>  findchecktable(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows){
			
				//进行分页
				Page<Object> pg = PageHelper.startPage(page, rows);
				JSONArray findAllPutInStorage = drugstorageservice.findchecktable();
				//进行数据格式转换和封装
				Map<String,Object> map = new HashMap<>();
				map.put("total", pg.getTotal());//封装总记录数
				map.put("rows", findAllPutInStorage);//封装当前页的内容
				
				return map;			
		}		
		
		//盘点审核
		@PostMapping("update-drugscheck")
		@ResponseBody
		public  String  updatedrugscheck(String name ,String zhaiyao ,Timestamp times,Integer checkid,BigDecimal jinec,Integer sumc){
				return	indentservice.update(name, zhaiyao, times, checkid, jinec, sumc);
		}	
		
		//修改库存数量
		@PostMapping("update-kusum")
		@ResponseBody
		public  JSONArray  updatekusum(Integer shul, Integer invsum, Integer pici){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = indentservice.updatekusum(shul, invsum, pici);
				return jsoninput;
		}		
		
		//盘点实际数量
		@PostMapping("update-take")
		@ResponseBody
		public  JSONArray  updatetake(Integer shijisum, Integer slcha, Integer biaozhi, Integer tid){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = indentservice.updatetake(shijisum, slcha, biaozhi,tid);
				return jsoninput;
		}		

		
		//查询盘点记录表
		@RequestMapping("find-drugcheck")
		@ResponseBody
		public  Map<String,Object>  finddrugcheck(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows){
			
				//进行分页
				Page<Object> pg = PageHelper.startPage(page, rows);
				JSONArray findAllPutInStorage = drugstorageservice.finddrugcheck();
				//进行数据格式转换和封装
				Map<String,Object> map = new HashMap<>();
				map.put("total", pg.getTotal());//封装总记录数
				map.put("rows", findAllPutInStorage);//封装当前页的内容
				
				return map;			
		}		

		//根据盘点单号查询盘点详情
		@PostMapping("find-checkdetails")
		@ResponseBody
		public  String  findcheckdetails(Integer drugchekid){
				JSONObject  json = new JSONObject();
				JSONArray jsoninput = drugstorageservice.findcheckdetails(drugchekid);
				json.put("result", jsoninput);
				return json.toJSONString();			
		}		

		// 供应商管理
		@RequestMapping("ph-suppliers")
		public void phSuppliers() {
				
		}
	
		//添加供应商
		@PostMapping("add-supplier")
		public void  AddSupplier(Supplier supplier) {
			supplierservice.AddSupplier(supplier);
		}
	
		//显示所有供应商
		@RequestMapping("findall-supplier")
		@ResponseBody
		public  Map<String,Object>  Findallsupplier(
				@RequestParam("page")int page,
				@RequestParam("rows")int rows){
			
				//进行分页
				Page<Object> pg = PageHelper.startPage(page, rows);
				List<Supplier> list = supplierservice.FindAllSupplier();
				
				//进行数据格式转换和封装
				Map<String,Object> map = new HashMap<>();
				map.put("total", pg.getTotal());//封装总记录数
				map.put("rows", list);//封装当前页的内容
								
				return map;
				
		}
		
		//修改供应商
		@PostMapping("update-supp")
		@ResponseBody
		public  JSONArray  updatedsuppliers(String phone,String people,Integer numb){
			JSONObject  json = new JSONObject();
			JSONArray jsoninput = indentservice.updatedsuppliers(phone, people, numb);
			return jsoninput;
			
		}		

}
